//
//  MSFilterViewController.m
//  JYG_Agent
//
//  Created by apple on 15/10/19.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "MSFilterViewController.h"
#import "MSFilterTableViewController.h"
#import "MSFilterBar.h"

const CGFloat barHeigth = 35;

@interface MSFilterViewController ()
//筛选视图灰色背景
@property UIView* backgroundView;
//弹出的筛选列表的视图容器
@property UIView* contentView;
//筛选列表
@property MSFilterTableViewController* listVctr;
//无筛选bar时的确定按钮
 @property(nonatomic,strong)UIButton *confirmBtn;
//筛选列表中选中的数据
@property(nonatomic,strong)NSMutableDictionary* params;
//无筛选bar时临时存放选中数据的字典
@property (nonatomic)NSDictionary *tmpSelectedDic;
//筛选bar
@property MSFilterBar* filterBar;
@property(nonatomic)UIView *tabbarCoverView;
@end

@implementation MSFilterViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    //灰色背景
    self.backgroundView=[[UIView alloc]init];
    self.backgroundView.backgroundColor=[UIColor colorWithWhite:0.f alpha:0.4f];
    self.backgroundView.clipsToBounds = YES;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
    tapGesture.delegate = (id)self;
    [self.backgroundView addGestureRecognizer:tapGesture];
    
    //筛选列表
    self.listVctr=[[MSFilterTableViewController alloc]initWithStyle:UITableViewStylePlain];
    self.listVctr.view.frame=CGRectMake(0, 0, APPFRAME.size.width, 280);
    self.listVctr.delegate=(id)self;
    self.listVctr.cellHeight=44.0;
    self.listVctr.enableRefresh=NO;
    
    //筛选列表容器
    self.contentView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, APPFRAME.size.width, barHeigth)];
    self.contentView.backgroundColor=[UIColor whiteColor];
    self.contentView.clipsToBounds=YES;
    [self.contentView addSubview:self.listVctr.view];
    [self.backgroundView addSubview:self.contentView];
    
    if (self.barType==MSFilterBarTypeHorizontalBar) {       //有横向筛选bar时添加筛选bar
        self.backgroundView.frame = CGRectMake(0, barHeigth, self.visibleRect.size.width, self.visibleRect.size.height);
        self.listVctr.view.backgroundColor = [UIColor whiteColor];
        self.listVctr.showSeperatorLine = YES;
        self.filterBar=[[MSFilterBar alloc]initWithFrame:CGRectMake(0, 0, APPFRAME.size.width, barHeigth)];
        self.filterBar.delegate=(id)self;
        self.filterBar.backgroundColor = [UIColor colorWithRed:0.9725 green:0.9725 blue:0.9725 alpha:1];
        [self.view addSubview:self.filterBar];
        self.view.frame = CGRectMake(0, self.visibleRect.origin.y, APPFRAME.size.width, barHeigth);
    } else {    //无筛选bar时添加确定按钮
        self.listVctr.view.backgroundColor = [UIColor colorWithWhite:0.89 alpha:1];
        self.view.frame = self.visibleRect;
        self.backgroundView.frame = self.view.bounds;
    }
    self.view.clipsToBounds=YES;
    self.view.backgroundColor=[UIColor clearColor];
}

- (void)showInView:(UIView *)superView
{
    [superView addSubview:self.view];
    if (!self.backgroundView.superview) {
        [self.view addSubview:self.backgroundView];
        if (self.shouldCoverTabbar) {
            UIWindow *window = [UIApplication sharedApplication].windows[0];
            [window addSubview:self.tabbarCoverView];
        }
    }
    _contentView.frame=CGRectMake(0, 0, APPFRAME.size.width, 280);
    self.listVctr.view.frame=CGRectMake(0, 0, APPFRAME.size.width, 280);
    self.listVctr.dataArray = [self.dataArray mutableCopy];
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
//    if (self.params.count) {
//        NSInteger row = [((NSNumber *)[self.params allKeys][0]) integerValue];
//        indexPath = [NSIndexPath indexPathForRow:row inSection:0];
//    }
    self.listVctr.defaultSelected = indexPath;
}

- (void)dismiss
{
    if (self.barType==MSFilterBarTypeHorizontalBar) {
        [self scrollToolBarBtnRepeat];
    } else {
        if (self.shouldCoverTabbar) {
            [self.tabbarCoverView removeFromSuperview];
        }
        self.tmpSelectedDic = self.params;
        [self.listVctr dismissSubViews];
        [self.backgroundView removeFromSuperview];
        [self.view removeFromSuperview];
    }
}

- (void)resetParams
{
    self.params = nil;
    [self.filterBar resetBtnState];
    [self dismiss];
}

#pragma mark - setters
- (void)setTmpSelectedDic:(NSDictionary *)tmpSelectedDic
{
    _tmpSelectedDic = tmpSelectedDic;
    self.listVctr.selectedDic = tmpSelectedDic;
}

- (void)setTitleArray:(NSArray *)titleArray
{
    _titleArray = [titleArray copy];
    self.filterBar.dataArray = titleArray;
}

- (void)confirmAction:(UIButton*)btn
{
    self.params = [self.tmpSelectedDic mutableCopy];
    if (btn.tag==0) {
        self.params = nil;
    }
    self.triggerBtn.selected = self.params.count;
    if (btn.tag==1) {
        if ([self.delegate respondsToSelector:@selector(selectedFilterWithParmas:)]) {
            [self.delegate selectedFilterWithParmas:self.params];
        }
    }
    [self dismiss];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    CGPoint touchPoint = [touch locationInView:self.backgroundView];
    return !CGRectContainsPoint(self.contentView.frame, touchPoint);
}

#pragma mark - GXScrollToolBarDelegate
- (void)scrollToolBarBtnRepeat
{
    if (self.shouldCoverTabbar) {
        [self.tabbarCoverView removeFromSuperview];
    }
    [self.backgroundView removeFromSuperview];
    self.view.frame = CGRectMake(0, self.visibleRect.origin.y, APPFRAME.size.width, self.filterBar.height);
    self.filterBar.curSeletedBtn.selected=NO;
}

- (void)scrollToolBarBtnAction:(GXButton*)sender toolBar:(id)toolBar
{
    self.view.frame = self.visibleRect;
    if (!self.backgroundView.superview) {
        [self.view addSubview:self.backgroundView];
        if (self.shouldCoverTabbar) {
            UIWindow *window = [UIApplication sharedApplication].windows[0];
            [window addSubview:self.tabbarCoverView];
        }
    }
    NSArray *dataArray = self.dataArray[sender.tag-100];
    CGFloat height = self.listVctr.cellHeight*dataArray.count>280?280:self.listVctr.cellHeight*dataArray.count;
    self.contentView.frame=CGRectMake(0, -height, APPFRAME.size.width, height);
    self.listVctr.view.frame=self.contentView.bounds;
    self.listVctr.dataArray = [dataArray mutableCopy];
    self.listVctr.selected = [self.params objectForKey:[NSNumber numberWithInteger:sender.tag-100]];
    
    [UIView animateWithDuration:0.3 animations:^
     {
         self.contentView.frame=CGRectMake(0, 0, APPFRAME.size.width, height);
     }];
}

- (void)tableView:(UITableView *)tableView didSelectedData:(NSString *)data
{
    if (self.barType==MSFilterBarTypeHorizontalBar) {
        if (!self.params) {
            self.params = [[NSMutableDictionary alloc] initWithCapacity:0];
        }
        [self performSelector:@selector(scrollToolBarBtnRepeat) withObject:nil afterDelay:0.1];
        NSString* str=((NSString* )self.listVctr.selected);
        [self.filterBar setTitle:str forBtn:(GXButton *)self.filterBar.curSeletedBtn];
        [self.params setObject:data forKey:[NSNumber numberWithInteger:self.filterBar.curSeletedBtn.tag-100]];
        if ([self.delegate respondsToSelector:@selector(selectedFilterWithParmas:)]) {
            [self.delegate selectedFilterWithParmas:self.params];
        }
        return;
    }
    if ([data isKindOfClass:[NSDictionary class]]) {
        if (!self.tmpSelectedDic) {
            self.tmpSelectedDic = [[NSDictionary alloc] init];
        }
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:self.tmpSelectedDic];
        [dic setValuesForKeysWithDictionary:(NSDictionary *)data];
        self.tmpSelectedDic = dic;
    }
}

- (UIView *)tabbarCoverView
{
    if (!_tabbarCoverView) {
        _tabbarCoverView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREENFRAME.size.height-KTabBarHeight, APPFRAME.size.width, KTabBarHeight)];
        _tabbarCoverView.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.4f];
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
        [_tabbarCoverView addGestureRecognizer:tapGesture];
    }
    return _tabbarCoverView;
}

-(void)dealloc
{
    self.filterBar=nil;
    self.backgroundView=nil;
    self.listVctr=nil;
}

@end
